package com.andnnl.pool.handle;

import com.andnnl.pool.Msg;
import com.andnnl.pool.TaskManage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 默认单个有问题重试单个
 * Created by chenss on 2022/5/31.
 */
public abstract class RetryMsgHandle<T> extends AbsMsgHandle<T> {
    private static Logger logger = LoggerFactory.getLogger(RetryMsgHandle.class);

    @Override
    public void handleMsg(TaskManage<T> taskManage, List<Msg<T>> msgs) {
        List<T> list = msgs.stream().map(m -> m.getData()).collect(Collectors.toList());
        for (Msg<T> msg : msgs) {
            try {
                handleMsg(msg.getData());
            } catch (Exception e) {
                logger.error("catch Exception:" + e.getMessage() + " list.size:" + list.size(), e);
                boolean isAdd = taskManage.addRetryMsg(msg);//没配置重试机制报IllegalArgumentException
                logger.info("retryTimes " + msg.getRetryTimes() + ",key:" + msg.getUserName() + ",isAdd:" + isAdd);
            }
        }
    }

    protected abstract void handleMsg(T m);



}
